//
//  QKTestReflectCtrl.m
//  testTool
//
//  Created by Su on 2019/5/27.
//  Copyright © 2019 com.qiankun. All rights reserved.
//

#import "QKTestReflectCtrl.h"

@interface QKTestReflectCtrl ()

@end

@implementation QKTestReflectCtrl


/**
 // SEL和字符串转换
 FOUNDATION_EXPORT NSString *NSStringFromSelector(SEL aSelector);
 FOUNDATION_EXPORT SEL NSSelectorFromString(NSString *aSelectorName);
 // Class和字符串转换
 FOUNDATION_EXPORT NSString *NSStringFromClass(Class aClass);
 FOUNDATION_EXPORT Class __nullable NSClassFromString(NSString *aClassName);
 // Protocol和字符串转换
 FOUNDATION_EXPORT NSString *NSStringFromProtocol(Protocol *proto) NS_AVAILABLE(10_5, 2_0);
 FOUNDATION_EXPORT Protocol * __nullable NSProtocolFromString(NSString *namestr) NS_AVAILABLE(10_5, 2_0);
 
 */


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.


}
-(void)testRefLection{
    NSDictionary *confParams = @{
                                 @"className":@"QKTestReflectCtrl",
                                 @"propertys":@{@"name":@"suqiankun",@"age":@"23"},
                                 @"method":@"addButton"
                                 };
    
    Class class = NSClassFromString(confParams[@"className"]);
    UIViewController *ctrl = [[class alloc] init];
    NSDictionary*params = confParams[@"propertys"];
    [params enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
        if ([ctrl respondsToSelector:NSSelectorFromString(key)]) {
            [ctrl setValue:obj forKey:key];
        }
    }];
    [self.navigationController pushViewController:ctrl animated:YES];
    SEL selector = NSSelectorFromString(confParams[@"method"]);
    [ctrl performSelector:selector];
    
}

-(void)addButton{
    
    UIButton *btn = [[UIButton alloc] init];
    [btn setTitle:self.name forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(changeColor) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.centerY.mas_equalTo(self.view);
        make.size.mas_equalTo(CGSizeMake(60, 20));
    }];
}
-(void)changeColor{
    self.view.backgroundColor = UIColor.redColor;
}
@end
